package de.michaelhuebl.demo.helper;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import de.michaelhuebl.demo.Location;

public class parseXmlWithSax extends DefaultHandler {

	ArrayList<Location> locations = new ArrayList<Location>();
	Boolean tagTitle = false;
	Boolean tagBusinessClickUrl = false;
	Location l;

	/**
	 * Gets called, whenever a new tag is found.
	 */
	@Override
	public void startElement(String uri, String localName, String name,	Attributes attributes) throws SAXException {
		
		// If <Result>-tag was found, a new location must be created.
		if (localName.equals("Result")) {
			l = new Location();
		}
		
		// A <title>-tag was found. Start recording text in characters().
		if (localName.equals("Title")) {
			tagTitle = true;
		}
		
		// A <BusinessClickUrl>-tag was found. Start recording text in characters().
		if (localName.equals("BusinessClickUrl")) {
			tagBusinessClickUrl = true;
		}
	}
	
	/**
	 * Gets called, whenever a closed tag is found.
	 */
	@Override
	public void endElement(String uri, String localName, String name) throws SAXException {
		// <Result>-tag is closed. Save the location, as there will be
		// a new location created when the next <Result> is found.
		if (localName.equals("Result")) {
			locations.add(l);
		}
		
		// A closed <title>-tag was found. Don't record text anymore.
		if (localName.equals("Title")) {
			tagTitle = false;
		}
		
		// A closed <BusinessClickUrl>-tag was found. Don't record text anymore.
		if (localName.equals("BusinessClickUrl")) {
			tagBusinessClickUrl = false;
		}
	}

	/**
	 * Gets called whenever text is found. For example between  <Title> and </Title>
	 */
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {

		// The text is between the Title-tags. Record it and save it in the object.
		if (tagTitle == true) {
			String s = new String(ch,start,length).trim();
			l.setTitle(s);
		}
		
		// The text is between the Title-tags. Record it and save it in the object.
		if (tagBusinessClickUrl == true) {
			String s = new String(ch,start,length).trim();
			l.setBusinessClickUrl(s);
		}
	}

	/**
	 * Parses a XML with SAX.
	 * Call getParseResult() to get the result.
	 * 
	 * @param xml XML-Code that should be parsed.
	 */
    public void parseXml(String xml) {
    	try {
            // New SAX parser
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser;
			saxParser = factory.newSAXParser();

			// Parse xml file with give XML
            saxParser.parse(new InputSource(new StringReader(xml)), this);
         
		} catch (ParserConfigurationException e) {
			Log.v("Demo", "ParserConfigurationException " + e.toString());
		} catch (SAXException e) {
			Log.v("Demo", "ParserConfigurationException " + e.toString());
		} catch (IOException e) {
			Log.v("Demo", "ParserConfigurationException " + e.toString());
		}
    }
    
	/**
	 * Returns the result of xml parsing.
	 * Call this after calling parseXML().
	 * 
	 * @return ArrayList
	 */
	public ArrayList<Location> getParseResult() {
		return locations;
	}    
}
